package com.example.shixun.service.impl;

import com.example.shixun.dao.CourseDao;
import com.example.shixun.pojo.Course;
import com.example.shixun.service.CourseService;
import com.example.shixun.utils.PageInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author XDW
 * @Version 1.0
 * @date 2023/6/21 16:27
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseDao courseDao;

    @Override
    public Integer saveCourse(Course course) {
        return courseDao.saveCourse(course);
    }

    @Override
    public Integer deleteCourseById(int id) {
        return courseDao.deleteCourseById(id);
    }

    @Override
    public Integer deleteCourseByName(String name) {
        return courseDao.deleteCourseByName(name);
    }

    @Override
    public Course selectCourseById(int id) {
        return courseDao.selectCourseById(id);
    }

    @Override
    public Course selectCourseByName(String name) {
        return courseDao.selectCourseByName(name);
    }

    @Override
    public PageInfoUtil<Course> selectCourseAll(PageInfoUtil<Course> pageInfoUtil) {
        //该语句一定要在执行sql 语句之前，参数第一个参数代表当前第几页，第二个参数代表每页的数量
        PageHelper.startPage(pageInfoUtil.getPageNum(),pageInfoUtil.getPageSize(), pageInfoUtil.getOrderBy());
        System.out.println("orderBy:"+pageInfoUtil.getOrderBy());
        System.out.println("course:"+pageInfoUtil.getParams());

        List<Course> lists=courseDao.selectCourseAll(pageInfoUtil.getParams());
        PageInfo<Course> pageInfo=  new PageInfo(lists);
        //System.out.println(pageInfo);
        pageInfoUtil.setPageNum(pageInfo.getPageNum());
        pageInfoUtil.setPageSize(pageInfo.getPageSize());
        pageInfoUtil.setPageTotal(pageInfo.getPages());
        pageInfoUtil.setTotal(pageInfo.getTotal());
        pageInfoUtil.setList(lists);
        return pageInfoUtil;
    }

    @Override
    public Integer updateCourse(Course course) {
        return courseDao.updateCourse(course);
    }

    @Override
    public List<Course> findByTeacherId(Course course) {
//        System.out.println("ID="+id+" status="+status);
        return courseDao.findByTeacherId(course);
    }

    @Override
    public Integer addStudent(Integer id) {
        return courseDao.addStudent(id);
    }

    @Override
    public Integer subStudent(Integer id) {
        return courseDao.subStudent(id);
    }

    @Override
    public Integer addLike(Integer id) {
        return courseDao.addLike(id);
    }

    @Override
    public Integer subLike(Integer id) {
        return courseDao.subLike(id);
    }
}
